﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for D_Employee
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{
    public class D_Employee
    {
        SqlDataReader rdR_Status;
        public D_Employee()
        {
            //
            // TODO: Add constructor logic here
            //
        }

       

        public Entities.E_Employee Employee_return(string Emp_id) 
        {
            //List<Entities.E_Employee> emp_list = new List<Entities.E_Employee>();
            //emp_list.Clear();
            Entities.E_Employee emp_entity = new Entities.E_Employee();
             try
             { //try

                 String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                 Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                
                 String Ssql = "SELECT * from dbo.Tbl_Employee  where Employee_Id='" + Emp_id + "'";
                 //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);


                 rdR_Status = clsCon.ReadTable(Ssql);


                 while (rdR_Status.Read())
                 {
                     emp_entity.Employee_iD = rdR_Status["Employee_Id"].ToString();
                     emp_entity.Employee_name = rdR_Status["Employee_Name"].ToString();
                     emp_entity.T_PV = decimal.Parse(rdR_Status["T_PV"].ToString());
                     emp_entity.T_bv = decimal.Parse(rdR_Status["T_BV"].ToString());
                     emp_entity.T_commision = decimal.Parse(rdR_Status["T_commision"].ToString());
                     emp_entity.T_price = decimal.Parse(rdR_Status["TotalPrice"].ToString());
                     emp_entity.Voucher_count = decimal.Parse(rdR_Status["voucher_count"].ToString());
                   
                     
                 }

             } //try

             catch (Exception err)
             {

                 //Response.Redirect("404.aspx?Massage=" + err.Message.ToString());

             }


             finally
             {
                 clsCon.conn_new.Close();
                 rdR_Status.Close();

             }   //finally



             return emp_entity;
        }

        #region Update Employee Table

        public Boolean Employee_tableUp(Entities.E_Employee empType)
        {
            Boolean upd2 = false;

            try
            {
                decimal Calc_commision = 0;

                string Emp_Id= empType.Employee_iD;
                string Emp_Name = empType.Employee_name;
                decimal Total_PV = empType.T_PV;
                decimal Total_BV = empType.T_bv;
                decimal Total_Price = empType.T_price;

                #region Getdatabase values
                Entities.E_Employee employee_e = new Entities.E_Employee();

                employee_e=Employee_return(Emp_Id);

                decimal DB_PV=employee_e.T_PV;
                decimal DB_BV = employee_e.T_bv;
                decimal DB_Price = employee_e.T_price;
                decimal DB_Commision = employee_e.T_commision;

                #endregion

                #region TCommision For Employee
                double Emp_comm =0.20;
                double commi=double.Parse(Total_BV.ToString()) * Emp_comm;
                Calc_commision = Math.Round(decimal.Parse(commi.ToString()), 3);             
                #endregion

                #region To database

                decimal To_PV = Total_PV + DB_PV;
                decimal To_BV = Total_BV + DB_BV;
                decimal To_commision = DB_Commision + Calc_commision;
                decimal To_Price = DB_Price + Total_Price;

                #endregion



                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String updtxt2 = "UPDATE Tbl_Employee SET  Employee_Name ='" + Emp_Name + "',T_PV ='" + To_PV + "',T_BV ='" + To_BV + "',T_commision ='" + To_commision + "',TotalPrice ='" + To_Price + "',Last_Modify='" + System.DateTime.Now + "' WHERE Employee_Id='" + Emp_Id + "'";
                // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                upd2 = clsCon.AddEditDel(updtxt2);

            }

            catch (Exception err)
            {


            }

            finally
            {
                clsCon.conn_new.Close();

            }

            return upd2;
        }

        #endregion

    }
}